package com.bjb.vr.common.service;

import com.bjb.vr.common.ao.OptCusAccountAo;
import com.bjb.vr.common.constant.Constants;
import com.bjb.vr.common.constant.RoleConstants;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.UmsAccount;
import com.bjb.vr.common.dto.UmsRole;
import com.bjb.vr.common.dto.UmsUserRole;
import com.bjb.vr.common.mapper.UmsAccountMapper;
import com.bjb.vr.common.mapper.UmsRoleMapper;
import com.bjb.vr.common.mapper.UmsUserRoleMapper;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.OptCusAccountVo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/6/17
 */
@Service
public class OptCusAccountService {

    private PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

    @Autowired
    private UmsRoleMapper umsRoleMapper;

    @Autowired
    private UmsAccountMapper umsAccountMapper;


    @Autowired
    private UmsUserRoleMapper umsUserRoleMapper;

    /**
     * 查询用户信息
     *
     * @param optCusAccountAo
     * @return
     */
    public List<OptCusAccountVo> queryUserRoleInfo(OptCusAccountAo optCusAccountAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);

        MPJLambdaWrapper<OptCusAccountVo> joinQueryWrapper = new MPJLambdaWrapper<>();
        joinQueryWrapper
                .select(UmsAccount::getId)
                .select(UmsAccount::getUserId)
                .select(UmsAccount::getUsername)
                .select(UmsRole::getRoleName)
                .select(UmsAccount::getStatus)
                .leftJoin(UmsUserRole.class, UmsUserRole::getUserId, UmsAccount::getUserId)
                .leftJoin(UmsRole.class, UmsRole::getRoleId, UmsUserRole::getRoleId);
        joinQueryWrapper.eq(UmsAccount::getTenantId, optCusAccountAo.getTenantId());
        List<OptCusAccountVo> list = umsAccountMapper.selectJoinList(OptCusAccountVo.class, joinQueryWrapper);

        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
        return list;
    }

    /**
     * 判断是否为超级管理员
     *
     * @param userId
     * @param userName
     * @return
     */
    public boolean checkSuperAdmin(Long userId, String userName) {
        MPJLambdaWrapper<UmsRole> joinQueryWrapper = new MPJLambdaWrapper<>();
        joinQueryWrapper
                .select(UmsRole::getRoleId)
                .select(UmsRole::getRoleName)
                .select(UmsRole::getRoleKey)
                .leftJoin(UmsUserRole.class, UmsUserRole::getRoleId, UmsRole::getRoleId)
                .leftJoin(UmsAccount.class, UmsAccount::getUserId, UmsUserRole::getUserId);
        joinQueryWrapper.eq(Objects.nonNull(userId), UmsAccount::getUserId, userId);
        joinQueryWrapper.eq(StringUtils.isNotBlank(userName), UmsAccount::getUsername, userName);
        // 执行查询
        UmsRole umsRole = umsRoleMapper.selectOne(joinQueryWrapper);
        if (Objects.isNull(umsRole)) {
            return false;
        }
        if (umsRole.getRoleKey().equals(RoleConstants.SUPER_ADMIN) || umsRole.getRoleName().equals(RoleConstants.SUPER_IDENTIFY)) {
            return true;
        }
        return false;
    }


    /**
     * 更新用户状态
     *
     * @param optCusAccountAo
     * @return
     */
    public boolean updateUserStatus(OptCusAccountAo optCusAccountAo) {
        UmsAccount umsAccount = new UmsAccount();
        umsAccount.setUserId(optCusAccountAo.getId());
        umsAccount.setStatus(optCusAccountAo.getStatus());
        // 更新用户状态
        return umsAccountMapper.updateById(umsAccount) == 1;
    }


    /**
     * 更新用户密码
     *
     * @param optCusAccountAo
     * @return
     */
    public boolean updateUserPassword(OptCusAccountAo optCusAccountAo) {
        UmsAccount umsAccount = new UmsAccount();
        umsAccount.setUserId(optCusAccountAo.getId());
        if (StringUtils.isNotBlank(optCusAccountAo.getPassword())) {
            umsAccount.setPassword(passwordEncoder.encode(optCusAccountAo.getPassword()));
            umsAccount.setInitialPass(Constants.NO);
        }
        // 更新用户密码
        return umsAccountMapper.updateById(umsAccount) == 1;
    }

}
